﻿/**
 * author : Frank Luo
 */
Ext.Loader.setConfig({
	enabled : true
});
// Ext.Loader.setPath('Ext.ux', '/TEX/resources/extjs/ux');
Ext.Loader.setPath('Ext.ux', ctx + '/resources/extjs/ux');
Ext.require([ 'Ext.ux.form.MultiSelect', 'Ext.ux.form.ItemSelector' ]);

Ext.define('HIT.view.SetRuleReplaceUnitWin', {
	extend : 'Ext.window.Window',
	alias : 'widget.replaceUnitWin',
	id:'replaceUnitWin',
	height:420,
	width :555,
	autoScroll:true,
	resizable:false,
	overlapHeader : false,
	preventHeader : true,
	title : Share.T('替换单位'),

	initComponent : function() {
		var me = this;
		this.title = Share.T('替换单位');
		var myId=1;//用于记录增加删除的fieldset的ID的参数
		var control=0;//用于控制增加位置的参数
		
		Ext.apply(Ext.form.VTypes, {
			confirmpsw : function(val, field) {
				if (field.confirmTo) {
					var pwd = Ext.getCmp(field.confirmTo);
					return (val == pwd.getValue());
				}
				return true;
			}
		});

		Ext.apply(Ext.form.VTypes, {
			daterange : function(val, field) {
				var date = field.parseDate(val);

				// We need to force the picker to update values to
				// recaluate the disabled dates display
				var dispUpd = function(picker) {
					var ad = picker.activeDate;
					picker.activeDate = null;
					picker.update(ad);
				};

				if (field.startDateField) {
					var sd = Ext.getCmp(field.startDateField);
					sd.maxValue = date;
					if (sd.menu && sd.menu.picker) {
						sd.menu.picker.maxDate = date;
						dispUpd(sd.menu.picker);
					}
				} else if (field.endDateField) {
					var ed = Ext.getCmp(field.endDateField);
					ed.minValue = date;
					if (ed.menu && ed.menu.picker) {
						ed.menu.picker.minDate = date;
						dispUpd(ed.menu.picker);
					}
				}
				return true;
			}
		});
		Ext.applyIf(me, {
			items : new Ext.form.Panel({
				xtype : 'fieldset',
				id:'replace_unit',
				items : [ {//第一个框 
					xtype : 'fieldset',
					layout : {
						type : 'absolute'
					},
					border : false,
					height : 130,
					items : [ {
						xtype : 'label',
						x : 5,
						y : 20,
						height : 15,
						text : Share.T('规则ID') + ':'
					}, {
						xtype : 'label',
						id:'replace_unit_ruleId',
						x : 50,
						y : 20,
						height : 15,
						text : ''
					}, {
						xtype : 'label',
						x : 95,
						y : 20,
						height : 15,
						text : Share.T('规则类型') + ':      ' + Share.T('替换单位')
					},{
			        	xtype : 'combobox',
			        	x : 255,
						y : 17,
						width:240,
						labelWidth:80,
						editable : false,
						fieldLabel : Share.T('有效范围'),
						id : 'replace_unit_scopeFlag',
						valueField : 'value',
						displayField : 'text',
						value:0,
						value:'0',
						store: new Ext.data.JsonStore({  
	    					proxy: { 
	        	                type: 'ajax', 
	        	                url : ctx + '/getComboData',
	        	                params:{
	        	                },
	        	                reader: { 
	        	                    type: 'json', 
	        	                    totalProperty: '', 
	        	                    root: '' 
	        	                } 
	        	             }, 
	        	            fields:['text','value'], 
	        	            autoLoad:true 
				        }),
				        listeners : {
				        	beforerender:function(){
				        		this.store.removeAll();
				        		this.store.on('beforeload', function(store) {
				        			Ext.apply(store.proxy.extraParams , {
				        				type : 'rulescope'
				        			});
				        		});
				        		this.store.load();
				        	}
				        },
			          onChange:function(){
							if(this.value=='1'){
								Ext.getCmp('replace_unit_custCode').setValue("");
								Ext.getCmp('replace_unit_custName_chn').setValue("");
								Ext.getCmp('replace_unit_custCode').disable();
								Ext.getCmp('replace_unit_custName_chn').disable();
								Ext.getCmp('replace_unit_set').enable();
							}else if (this.value == '2'){
								Ext.getCmp('replace_unit_custCode').enable();
								Ext.getCmp('replace_unit_custName_chn').enable();
								Ext.getCmp('replace_unit_set').disable();
							} else {
								Ext.getCmp('replace_unit_set').enable();
								Ext.getCmp('replace_unit_custCode').enable();
								Ext.getCmp('replace_unit_custName_chn').enable();
							}
						}
			      },{
			            xtype: 'hiddenfield',
			            x:700,
			            id: 'replace_unit_hiddenFieldValue',
			            value: -1
					},{
						xtype : 'combobox',
						id:'replace_unit_custCode',
						triggerAction : 'query',
						x : 5,
						y : 50,
						labelWidth : 60,
						height : 15,
						width : 220,
						editable : true,
						minChars : 1,
						fieldLabel : Share.T('客户'),
						selectOnFocus : true,
						valueField : 'name',
						displayField : 'code',
						hideTrigger : true, // 隐藏下拉箭头
						typeAhead : false,
						queryDelay : 20,
						msgTarget  : 'side',						
						allowBlank : false,
						store : new Ext.data.JsonStore({
							proxy: { 
								type: 'ajax', 
								url : ctx + '/getCustInfo',
								params:{
								},
								reader: { 
									type: 'json', 
									totalProperty: '', 
									root: '' 
								} 
							}, 
						fields:['code','name'], 
      	                autoLoad:true 
				        }),
	      				listeners : {    					
	    					beforequery : function(e) {
	        					var combo = e.combo;   
	        					if(!e.forceAll){   
	        						var value = e.query;   
	        						combo.store.filterBy(function(record,id){   
	        							var text = record.get(combo.displayField); 
	        							return (text.indexOf(value)!=-1);   
	        						});
	        						combo.expand();   
	        						return false;   
	        					}
	    					},
	    					select:function(e){
				      			    var name=Ext.getCmp('replace_unit_custCode').getValue(); //得到此下拉框的value				      			    
				      			    Ext.getCmp("replace_unit_custName_chn").setValue(name);//设定label的text
				             }			            
				       }
					}, {
						xtype : 'combobox',
						triggerAction : 'query',
						id:'replace_unit_custName_chn',
						allowBlank : false,
						msgTarget  : 'side',
						hideTrigger : true, // 隐藏下拉箭头
						selectOnFocus : true,
						fieldLabel : Share.T('客户名称'),
						x : 255,
						y : 50,
						labelWidth : 80,
						height : 15,
						width : 240,
						text : "",
						valueField : 'code',
						displayField : 'name',
						typeAhead : false,
						queryDelay : 20,
						store : new Ext.data.JsonStore({
							proxy: { 
								type: 'ajax', 
								url : ctx + '/getCustInfo',
								params:{
								},
								reader: { 
									type: 'json', 
									totalProperty: '', 
									root: '' 
								} 
							}, 
						fields:['code','name'], 
      	            autoLoad:true 
			          }),
			          listeners : {
	    					beforequery : function(e) {
	        					var combo = e.combo;   
	        					if(!e.forceAll){   
	        						var value = e.query;   
	        						combo.store.filterBy(function(record,id){   
	        							var text = record.get(combo.displayField); 
	        							return (text.indexOf(value)!=-1);   
	        						});
	        						combo.expand();   
	        						return false;   
	        					}
	    					},
	    					//选择后更改label的text
				      		select:function(e){
				      			var value=Ext.getCmp('replace_unit_custName_chn').getValue();//得到此下拉框的value
	        					Ext.getCmp("replace_unit_custCode").setValue(value);//设定label的text
				      		}
	    				}
					}, {
						xtype : 'textfield',
						id:'replace_unit_remarks',
						x : 5,
						y : 80,
						width : 490,
						fieldLabel : Share.T('备注'),
						labelWidth : 60,
						msgTarget  : 'side',
						maxLength :100
					} ]
				}, {//分摊条件
					xtype : 'fieldset',
					title : Share.T('条件'),
					id:'condition',
					width:500,
					x:5,
					items : [ {
						xtype : 'panel',
						margin: '10 0 10',
						id:'fieldset'+myId,
						layout : {
							type : 'absolute'
						},
						border : false,
						height: 24,
						items : [ {
							xtype : 'combobox',
							id:'replace_unit_mainCondition'+myId,
							triggerAction : 'all',
							x : 40,
							width : 120,
							editable : false,
							minChars : 1,
							selectOnFocus : true,
							valueField : 'value',
							displayField : 'code',
							typeAhead : false,
							queryDelay : 20,
							msgTarget  : 'side',
							store : new Ext.data.JsonStore({
								proxy: { 
									type: 'ajax', 
									url : ctx + '/getCommonConditions',
									params:{ 								
									},
									reader: { 
										type: 'json', 
										totalProperty: '', 
										root: '' 
									} 
								}, 
							fields:['value','code'], 
	      	                autoLoad:true 
				          })
						}, {
							xtype : 'combobox',
							id:'relace_unit_conditionChoice'+myId,
							triggerAction : 'all',
							x : 170,
							width : 85,
							editable : false,
							minChars : 1,
							selectOnFocus : true,
							valueField : 'value',
							displayField : 'condition',
							typeAhead : false,
							queryDelay : 20,
							msgTarget  : 'side',
							store : new Ext.data.JsonStore({
								proxy: { 
									type: 'ajax', 
									url : ctx + '/getCondition',
									params:{ 								
									},
									reader: { 
										type: 'json', 
										totalProperty: '', 
										root: '' 
									} 
								}, 
							fields:['condition','value'], 
	      	                autoLoad:true 
				          })
						}, {
							xtype : 'textfield',
							id:'replace_unit_conditionValue'+myId,
							x : 265,
							width : 100,
							msgTarget  : 'side',						
							maxLength:200
						}, {
							xtype : 'button',
							id:'delete',
							x : 375,
							width : 60,
							text : Share.T('删除'),
							onClick:function(){
								var condition=Ext.getCmp('condition');
								//删除本fieldset
								condition.remove(this.findParentByType('panel'));
								control--;
							}
						} ]
					},{
						xtype : 'panel',
						margin: '10 0 10',
						layout : {
							type : 'absolute'
						},
						height: 24,
						border:false,
						items : [ {
							xtype : 'button',
							id:'replaceUnit_condition_add',
							x : 375,
							width : 60,
							text : Share.T('增加'),
							handler:function(){
								myId++;
								var newField=new Ext.form.Panel({
								    margin: '10 0 10',
								    height: 24,
									id:'fieldset'+myId,
									layout : {
										type : 'absolute'
									},
									border : false,
									items : [ {
										xtype : 'combobox',
										id:'replace_unit_mainCondition'+myId,
										triggerAction : 'all',
										x : 40,
										width : 120,
										editable : false,
										minChars : 1,
										selectOnFocus : true,
										valueField : 'value',
										displayField : 'code',
										typeAhead : false,
										queryDelay : 20,
										msgTarget  : 'side',
										store : new Ext.data.JsonStore({
											proxy: { 
												type: 'ajax', 
												url : ctx + '/getCommonConditions',
												params:{ 								
												},
												reader: { 
													type: 'json', 
													totalProperty: '', 
													root: '' 
												} 
											}, 
										fields:['value','code'], 
				      	                autoLoad:true 
							          })
									}, {
										xtype : 'combobox',
										id:'relace_unit_conditionChoice'+myId,
										triggerAction : 'all',
										x : 170,
										width : 85,
										editable : false,
										minChars : 1,
										selectOnFocus : true,
										valueField : 'value',
										displayField : 'condition',
										typeAhead : false,
										queryDelay : 20,
										msgTarget  : 'side',
										store : new Ext.data.JsonStore({
											proxy: { 
												type: 'ajax', 
												url : ctx + '/getCondition',
												params:{ 								
												},
												reader: { 
													type: 'json', 
													totalProperty: '', 
													root: '' 
												} 
											}, 
										fields:['condition','value'], 
				      	                autoLoad:true 
							          })
									}, {
										xtype : 'textfield',
										id:'replace_unit_conditionValue'+myId,
										x : 265,
										width : 100,
										msgTarget  : 'side',						
										maxLength:200
									}, {
										xtype : 'button',
										x : 375,
										width : 60,
										text : Share.T('删除'),
										onClick:function(){
											var condition=Ext.getCmp('condition');
											condition.remove(this.findParentByType('panel'));
											control--;
										}
									} ]
								});
								control++;
								Ext.getCmp('condition').insert(control,newField);       
								Ext.getCmp('condition').doLayout();  
							}
						} ]
					} ]
				},{//转换处理
					xtype : 'fieldset',
					height : 80,
					id:'replace_unit_set',
					layout : {
						type : 'absolute'
					},
					width:500,
					x:5,
					title : Share.T('替换成'),
					items : [{xtype:'label',
						      x:80,
						      y:ChangeSystem.Explore == 'IE8' ? 2 : -2,
						      text:Share.T('转换前单位')
					},{
						xtype : 'textfield',
						id:'replace_unit_before',
						x : 60,
						y : 15,
						width : 100,
						msgTarget  : 'side',						
						allowBlank : false,
						maxLength:20
					},{
						xtype:'label',
						x:175,
						y:13,
						width:30,
						style:'font-size:20px',
						text:Share.T('⇒')
					},{
						xtype:'label',
						x:243,
						y:ChangeSystem.Explore == 'IE8' ? 2 : -2,
						text:Share.T('转换率')
					},{
						xtype : 'numberfield',
						id:'replace_unit_rate',
						msgTarget  : 'side',
						minValue: 0.001,
						maxValue:99999.999,
					    hideTrigger: true,
						decimalPrecision : 3 ,      
						regex : /^([1-9]\d*|0)(\.\d+)?$/,
						x : 210,
						y : 15,
						width : 100,
						msgTarget  : 'side',
						allowBlank : false
					},{
						xtype:'label',
						x:338,
						y:ChangeSystem.Explore == 'IE8' ? 2 : -2,
						text:Share.T('转换后单位')
					},{
						xtype : 'textfield',
						id:'replace_unit_after',
						x : 320,
						y : 15,
						width : 100,
						msgTarget  : 'side',						
						allowBlank : false,
						maxLength:20
					}]
				} ]
			}),
			buttons : [ {
				xtype : 'button',
				id:'replace_unit_add',
				width : 45,
				text : Share.T('保存')
			},{
				xtype : 'button',
				id:'replace_unit_copy',
				width : 45,
				text : Share.T('复制'),
				handler : function()
				{ 
					if(Ext.getCmp('replace_unit_hiddenFieldValue').getValue()!=-1)
						{
						   Ext.getCmp('replace_unit_ruleId').setText('');
						   this.disable();
						   Ext.getCmp('replace_unit_delete').disable();
						   Ext.getCmp('replace_unit_hiddenFieldValue').setValue(-1);
						   Ext.getCmp('replace_unit_scopeFlag').enable();
						   if(Ext.getCmp('replace_unit_scopeFlag').getValue() != '1'){
							   Ext.getCmp('replace_unit_custCode').enable();
							   Ext.getCmp('replace_unit_custName_chn').enable();
						   }
						}
					Share.ShowMessage('I0057');
				}
			},{
				xtype : 'button',
				id:'replace_unit_delete',
				width : 45,
				text : Share.T('删除')
			},{
				xtype : 'button',
				id:'replace_unit_cancel',
				width : 45,
				text : Share.T('取消')
			}]
		});
		me.callParent(arguments);
	}
});
